var mongoose=require('mongoose');
var Post=mongoose.model('Post',{
    title:String,
    desc:String,
    author:{
        name:String,
        age:Number
    },
    comments:[
        {
            content:String,
            like:Number
        }
    ]
});
var db=mongoose.connect('mongodb://localhost/MongoDB_QuanWei_2');

Post.update({
    title:'post 1'
},{
    author:{
        name:'Tom',
        age:20
    },
    comments:[
        {
            content:'c1',
            like:0
        }
    ]
},{upsert:true},(err,r)=>{
    console.log(err,r);

    Post.update({title:'post 1'},{$push:{comments:{content:'c2',like:33}}},(e,r)=>{
        console.log(e,r);//null { ok: 1, nModified: 1, n: 1 }


        Post.update({title:'post 1'},{
            $push:{
                comments:{
                    $each:[
                        {content:'c3',like:5},
                        {content:'c4',like:15},
                        {content:'c5',like:25}
                    ]
                }
            }
        },(e,r)=>{
            console.log(e,r);//null { ok: 1, nModified: 1, n: 1 }

            process.exit(1);
        });

    });
});

/* $push:{field:val}
 * 向field字段中追加一个值
 * */

/*
 * $push:{
    field:{
      $each:[val1,...]
    }
   }
 *
 * $each:与其它更新操作配合使用，一次处理多个值
 * */





